package cn.zhy.org.service.impl;

import cn.zhy.basic.exception.BusinessException;

import cn.zhy.basic.util.BaiduAuditUtils;
import cn.zhy.basic.util.MD5Utils;
import cn.zhy.basic.util.StrUtils;
import cn.zhy.org.domain.Employee;
import cn.zhy.org.domain.Shop;
import cn.zhy.org.domain.ShopAuditLog;
import cn.zhy.org.dto.ShopDto;
import cn.zhy.org.mapper.EmployeeMapper;
import cn.zhy.org.mapper.ShopAuditLogMapper;
import cn.zhy.org.mapper.ShopMapper;
import cn.zhy.org.service.IShopService;
import cn.zhy.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;


/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;



    @Override
    public void settlement(Shop shop) {
        //1.空值判断
        String address = shop.getAddress();
        String name = shop.getName();
        String tel = shop.getTel();
        if (StringUtils.isEmpty(address) || StringUtils.isEmpty(name) || StringUtils.isEmpty(tel)){
            throw new BusinessException("数据不能为空");
        }

        //2.判断密码是否一致

        //3.检验店铺是否已经入驻过-loadByNameAndAddress-在sql里查询出名字和地址一样就是入驻过的
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if (dbShop !=null){//入驻过
            throw new BusinessException("您已入驻，请直接登录");
        }

        //百度AI审核文本
        if (!BaiduAuditUtils.textCensor(shop.getName())){
            throw new BusinessException("每日一巡逻，名字不合格！");
        }

        //百度AI图片审核
        String imgUrl = "http://123.207.27.208" + shop.getLogo();
        if (!BaiduAuditUtils.imgCensor(imgUrl)) {
            throw new BusinessException("每日一巡逻，图片不合格！");
        }


        //4.保存信息
        //4.1保存管理员的信息
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);
        //处理盐值和加密
        String salt = StrUtils.getComplexRandomString(32);
        String md5 = MD5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5);
        //保存员工信息
        employeeMapper.save(admin);

        //4.2保存店铺信息-需要用到管理员的id
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);


        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //log里有shopId  note
        //获取shopid然后设置状态1->2
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //发送邮件
        //创建复杂邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方-获取管理员id
        helper.setFrom("2670964312@qq.com");
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //设置收件人-employy里管理员邮箱
        //helper.setTo(employee.getEmail());
        helper.setTo("2670964312@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>你的店铺已注册,点击激活!!!</h1>" +
                "<img src='http://123.207.27.208"+shop.getLogo()+"' >" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //发送
        javaMailSender.send(mimeMessage);

        //添加审核日志到数据库
        log.setState(2);
        log.setAuditId(325L);
        shopAuditLogMapper.save(log);

    }

    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //获取shopid然后设置状态1->2
        Shop shop = shopMapper.loadById(log.getShopId());
        //设置店铺的状态
        shop.setState(4);
        //调用修改传进去
        shopMapper.update(shop);

        //添加审核日志到数据库
        log.setState(4);
        log.setAuditId(325L);
        shopAuditLogMapper.save(log);

        //发送邮件
        //创建复杂邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方-获取管理员id
        helper.setFrom("2670964312@qq.com");
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //设置收件人-employy里管理员邮箱
        //helper.setTo(employee.getEmail());
        helper.setTo("2670964312@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>入驻失败!!!</h1>" +
                "<a href='http://localhost:8081/#/register'>点击重新入驻</a>",true);
        //发送
        javaMailSender.send(mimeMessage);
        //删除入驻信息-删除管理员信息
        employeeMapper.remove(shop.getAdminId());
        //删除入驻信息-删除店铺
        shopMapper.remove(shop.getId());

    }

    @Override
    public List<ShopDto> echar() {
        return shopMapper.echar();
    }

    @Override
    public void success(Long id) throws IOException {
        Shop shop = shopMapper.loadById(id);
        //设置店铺的状态
        shop.setState(3);
        //调用修改传进去
        shopMapper.update(shop);

        //添加审核日志到数据库
        ShopAuditLog log = shopAuditLogMapper.loadByShopId(shop.getId());
        log.setState(3);
        log.setAuditId(325L);
        shopAuditLogMapper.update(log);

    }


}
